package com.whh.cos.action;

import com.qcloud.cos.model.Bucket;
import com.whh.cos.service.BucketService;
import com.whh.cos.system.ServiceException;
import com.whh.cos.utils.Converter;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author huahui.wu.
 *         Created on 2018/1/16.
 */
@RestController
@RequestMapping(value = "/bucket")
public class BucketAction {

    @Resource
    private BucketService bucketService;

    @RequestMapping(value = "/getBuckets")
    public List<Bucket> getBuckets() {
        return bucketService.getBuckets();
    }

    @RequestMapping(value = "/createBucket")
    public Bucket createBucket(@RequestBody HashMap<String, Object> params) throws ServiceException {
        String bucketName = Converter.getString(params, ("bucketName"));
        if (bucketName == null) {
            throw new ServiceException(100101, "bucketName is null");
        }
        if (bucketService.headBucket(bucketName)){
            throw new ServiceException(100102, "bucket name is exist");
        }
        return bucketService.createBucket(bucketName);
    }

    @RequestMapping(value = "/deleteBucket")
    public void deleteBucket(@RequestBody HashMap<String, Object> params) throws ServiceException {
        String bucketName = String.valueOf(params.get("bucketName"));
        if (bucketName == null) {
            throw new ServiceException(100101, "bucketName is null");
        }
        if (!bucketService.headBucket(bucketName)){
            throw new ServiceException(100102, "bucket name is not exist");
        }
        bucketService.deleteBucket(bucketName);
    }

}
